package escritorio.Ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;

import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.JButton;

import escritorio.conn.ConnErro;
import escritorio.conn.ConnectionFactory;
import escritorio.conn.PrazoDao;
import escritorio.javaBeans.Prazos;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class PrazoFrm extends JFrame {

	private JPanel contentPane;
	private JTextField txtDataObjetivo;
	private JTextArea txtDescricao;
	private JComboBox cmbPrazo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					PrazoFrm frame = new PrazoFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PrazoFrm() {
		setTitle("Cadastro de prazo");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 601, 518);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panelGeral = new JPanel();
		contentPane.add(panelGeral, BorderLayout.CENTER);
		panelGeral.setLayout(null);

		JLabel lblProcesso = new JLabel("Processo :");
		lblProcesso.setBounds(12, 13, 60, 16);
		panelGeral.add(lblProcesso);

		cmbPrazo = new JComboBox();
		cmbPrazo.setBounds(109, 10, 195, 22);
		fillcmbPrazo();
		panelGeral.add(cmbPrazo);

		JLabel lblDataObjetivo = new JLabel("Data objetivo :");
		lblDataObjetivo.setBounds(12, 48, 84, 16);
		panelGeral.add(lblDataObjetivo);

		txtDataObjetivo = new JTextField();
		txtDataObjetivo.setToolTipText("Data no formato dd/mm/aaaa");
		txtDataObjetivo.setBounds(109, 45, 116, 22);
		panelGeral.add(txtDataObjetivo);
		txtDataObjetivo.setColumns(10);

		JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o :");
		lblDescrio.setBounds(12, 77, 69, 16);
		panelGeral.add(lblDescrio);

		JButton btnIncluir = new JButton("Incluir");
		btnIncluir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				adicionaPrazo();
			}
		});
		btnIncluir.setBounds(323, 9, 97, 25);
		panelGeral.add(btnIncluir);

		JButton btnEditar = new JButton("Editar");
		btnEditar.setBounds(323, 44, 97, 25);
		panelGeral.add(btnEditar);

		JButton btnConsultar = new JButton("Consultar");
		btnConsultar.setBounds(323, 77, 97, 25);
		panelGeral.add(btnConsultar);

		JButton btnSair = new JButton("Sair");
		btnSair.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnSair.setBounds(460, 44, 97, 25);
		panelGeral.add(btnSair);

		JPanel panelDescricao = new JPanel();
		panelDescricao.setBounds(22, 117, 539, 333);
		panelGeral.add(panelDescricao);
		panelDescricao.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBounds(0, 0, 539, 333);
		panelDescricao.add(scrollPane);

		txtDescricao = new JTextArea();
		txtDescricao.setFont(new Font("Monospaced", Font.PLAIN, 18));
		scrollPane.setViewportView(txtDescricao);

	}
	
	private void fillcmbPrazo() {
		String sql = "select nroProcesso from Processo";
		try {
			Connection connection = new ConnectionFactory().getConnection();
			PreparedStatement stmt = connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				String proc = rs.getString("nroProcesso");
				cmbPrazo.addItem(proc);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			throw new ConnErro(null, e);
		}
	}

	protected void adicionaPrazo() {
		Prazos prazo = new Prazos();
		PrazoDao daoPz = new PrazoDao();
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat data = new SimpleDateFormat("dd/MM/yyyy");
		try {
			cal.setTime(data.parse(txtDataObjetivo.getText()));
		} catch (ParseException e) {
			System.out.println("Erro no parse da data calendar em PrazoFrm");
			e.printStackTrace();
		}
		prazo.setDataObj(cal);
		prazo.setDescricao(txtDescricao.getText());
		daoPz.inserePrazo(prazo);

		System.out.println("Gravado!");
	}
}
